/*
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company: CoE
 * @author: Old developer team.
 * @version 1.1
 */

package wads.app;

import java.awt.Rectangle;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.Properties;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import wads.com.UserInfo;
import wads.com.WADTools;
import wads.drawing.node.UserNode;

public class UserNodeDialog extends BaseWADDialog {
//  private static final long serialVersionUID = -7231201808055400565L;
  
  private JLabel usernameLabel = new JLabel("Username");
  private JComboBox choice1 = new JComboBox();

  private ObjectInputStream ois;
  private UserInfo ui;

  private UserNode user;
  
  private static final int NODE_DIALOG_HEIGHT = 270;

  public UserNodeDialog(CaseEditor parent, UserNode owner) {
    super(parent, owner);
    this.user = owner;
    this.setTitle("Set User to Node");
    ui = parent.getPUUserInfo();

    try{
      jbInit();
    }
    catch(Exception e) { e.printStackTrace(); }
  }// end of UserNodeDialog()

  private void jbInit() throws Exception {
    usernameLabel.setBounds(new Rectangle(10, 15, 80, 25));
    usernameLabel.setToolTipText("Select User from list.");
    this.getContentPane().setLayout(null);

    choice1.setBounds(new Rectangle(95, 15, 170, 25));
    //user = this.getListUser(this.getOrgName());
//    this.getListUser(this.getOrgName());
    for(int i = 0; i< ui.getSize(); i++) {
      choice1.addItem(ui.getUserDataAt(i).getName());
    }

    this.getContentPane().add(usernameLabel, null);
    this.getContentPane().add(choice1, null);
//    this.getListUser(getOrgName());
    this.setSize(350, NODE_DIALOG_HEIGHT);
    this.setLocation(10, 10);
    setOptionButtonsPanelLocation(NODE_DIALOG_HEIGHT);
  }// end of jbInit()

  @Override
  public void saveProperties(){
    String comName = choice1.getSelectedItem().toString();
    //check field of user
    System.out.println(comName);
    //properties is variable of BaseWADDialog Class
//    properties.setProperty("name", comName);
    user.setName(comName);
//    properties.setProperty("uname", usern.elementAt(choice1.getSelectedIndex()));
//    properties.setProperty("uname", ui.getUserDataAt(choice1.getSelectedIndex()).getUsername());
    user.setUsername(ui.getUserDataAt(choice1.getSelectedIndex()).getUsername());
  }

  @Override
  public void initDisplay(){
    choice1.setSelectedItem(user.getName());
  }

  public String getOrgName(){
    String orgName = null;
    try{
      //don't use 'properties' - may it not be initialized
      Properties p = new Properties();
      p.load(new FileInputStream(new File(WADTools.CONFIG_FILENAME)));
      orgName = p.getProperty("orgName", "");
      return orgName;
    }
    catch(Exception e) { e.printStackTrace(); }

    return orgName;
  }

  @Deprecated
  public Vector getListUser(String orgName){
    if(WADTools.checkPowerUserFile(orgName)){
      try{
        ois = new ObjectInputStream(new FileInputStream(WADTools.POWER_USER_PATH + orgName));
        ui = (UserInfo)ois.readObject();
        ois.close();
        
        for(int i=0;i<ui.getSize();i++){
          String tmpn = ui.getUserDataAt(i).getUsername();
          String tmp = ui.getUserDataAt(i).getName();
//          user.add(tmp);
//          usern.add(tmpn);
        }
      }
      catch(ClassNotFoundException cnfe) {
        System.err.println("UserInfo Class has problem");
      }
      catch(NullPointerException npe) {
        System.err.println("Can't get any users from file");
      }
      catch(Exception e) {
        e.printStackTrace();
      }
    }
    else
      System.out.println("Can't get User.");

    return null;
  }// end of getListUser()

    /**
     * @return the user
     */
    public UserNode getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(UserNode user) {
        this.user = user;
    }
}// end of UserNodeDialog Class